package server.ppt;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.imageio.ImageIO;

public class PptScaler implements Runnable {

	private String fromDir;
	private String toDir;
	private static int yOffset;
	private static int xOffset;
	public final static Rectangle screen = new Rectangle(Toolkit
			.getDefaultToolkit().getScreenSize());

	private static int scaledWidth;
	private static int scaledHeight;

	public PptScaler(String fromDir, String toDir) {
		this.fromDir = fromDir;
		this.toDir = toDir;
	}

	@Override
	public void run() {
		File from = new File(this.fromDir);
		File to = new File(this.toDir);
		if (to.exists()) {
			this.delAllSubFiles(to);
		}
		to.mkdir();
		File[] files = from.listFiles();
		try {
			BufferedImage tmp = ImageIO.read(files[0]);
			int height = tmp.getHeight();
			int width = tmp.getWidth();
			if (width * screen.getHeight() > screen.getWidth() * height) {
				scaledWidth = (int) screen.getWidth();
				scaledHeight = (int) ((screen.getWidth() * height) / width);
				yOffset = (int) ((screen.getHeight() - scaledHeight) / 2);

				System.out.println("is widescreen ppt:" + true);
			} else {
				scaledWidth = (int) ((screen.getHeight() * width * 1.0) / height);
				scaledHeight = (int) screen.getHeight();
				xOffset = (int) ((screen.getWidth() - scaledWidth) / 2);
				System.out.println("is widescreen ppt:" + false);
			}
			System.out.println("screen width:" + PptScaler.screen.getWidth());
			System.out.println("screen height:" + PptScaler.screen.getHeight());
			System.out.println("ppt width:" + scaledWidth);
			System.out.println("ppt height:" + scaledHeight);
			System.out.println("x offset:" + xOffset);
			System.out.println("y offset:" + yOffset);
			
			for (File file : files) {
				BufferedImage image = ImageIO.read(file);
				BufferedImage bImage = getScaledBufferedImage(image);
				ImageIO.write(bImage, "jpg",
						new File(toDir + "/" + file.getName()));
				file.delete();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void delAllSubFiles(File to) {
		for (File file : to.listFiles()) {
			file.delete();
		}
	}

	public static BufferedImage getScaledBufferedImage(BufferedImage image) {
		Image scaledImage = image.getScaledInstance(scaledWidth, scaledHeight,
				Image.SCALE_DEFAULT);
		BufferedImage bufImg = getBufferedImage(scaledImage);
		return bufImg;
	}

	private static BufferedImage getBufferedImage(Image scaledImage) {
		BufferedImage bufImg = new BufferedImage(scaledImage.getWidth(null),
				scaledImage.getHeight(null), BufferedImage.TYPE_INT_RGB);
		Graphics g = bufImg.createGraphics();
		g.drawImage(scaledImage, 0, 0, null);
		g.dispose();
		return bufImg;
	}

	public static int getYOffset() {
		return yOffset;
	}

	public static int getXOffset() {
		return xOffset;
	}
}
